﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using Newtonsoft.Json;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.WcfService.Contracts;

using System.Web.Script.Serialization;

using System.ServiceModel.Web;


namespace JM.JCY.WcfService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UserManagement : IUserManagement
    {

        public int IsExistDept(string DeptName)
        {

            int count = 0;
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                count = proxy.IsExistDept(DeptName);
            }
            return count;
        }

        public string GetTitleByDeptId(int deptId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                DataSetUserManagement.T_JobTitleDataTable dt = proxy.GetTitleByDeptId(deptId);
                var result = from dr in dt
                             select new
                             {
                                 ID = dr.ID,
                                 JobTitle = dr.JobTitle
                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public string IsExistUser(string userName)
        {
            int count = 0;
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetUser(0, userName);
                if (dt.Rows.Count > 0)
                    count = dt.Rows.Count;
            }
            return count.ToString();
        }

        public string GetResposibility(int deptId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetDeptById(deptId, string.Empty);
                var result = from dr in dt
                             select new
                             {
                                 deptResp = dr.DeptResponsibility
                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public void UpdateDeptResposibility(int deptId, string deptResp)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetDeptById(deptId, string.Empty);
                dt[0].DeptName = dt[0].DeptName;
                dt[0].DeptDesc = dt[0].DeptDesc;
                dt[0].DeptCulture = dt[0].DeptCulture;
                dt[0].SortOrder = dt[0].SortOrder;
                dt[0].DeptResponsibility = deptResp;
                dt[0].IsDeleted = false;
                proxy.UpdateDepartmentSortOrder(dt);
            }

        }

        public string GetCulture(int deptId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetDeptById(deptId, string.Empty);
                var result = from dr in dt
                             select new
                             {
                                 deptCulture = dr.DeptCulture
                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public void UpdateDeptCulture(int deptId, string deptCulture)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetDeptById(deptId, string.Empty);
                dt[0].DeptName = dt[0].DeptName;
                dt[0].DeptDesc = dt[0].DeptDesc;
                dt[0].DeptCulture = deptCulture;
                dt[0].SortOrder = dt[0].SortOrder;
                dt[0].DeptResponsibility = dt[0].DeptResponsibility;
                dt[0].IsDeleted = false;
                proxy.UpdateDepartmentSortOrder(dt);
            }

        }

        public string GetUserInfo(int userId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {

                var dt = proxy.GetUserForHome(userId);
                var result = from dr in dt
                             select new
                             {
                                 name = dr.UserName,
                                 pic = dr.PhotoImagePath,
                                 office = dr.OfficeAddress,
                                 officeNumber = dr.OfficeNumber,
                                 mobileNumber = dr.MobileNumber,
                                 homeNumber = dr.HomeNumber,
                                 email = dr.Email,
                                 dept = dr.DeptName

                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public void UpdateUserInfo(int userId, string office, string officeNumber, string mobileNumber, string homeNumber, string email)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetUser(userId, string.Empty);
                var row = dt[0];
                row.OfficeAddress = office;
                row.OfficeNumber = officeNumber;
                row.MobileNumber = mobileNumber;
                row.HomeNumber = homeNumber;
                row.Email = email;
                // original data
                row.UserName = row.UserName;
                if (!row.IsGenderNull())
                    row.Gender = row.Gender;
                if (!row.IsDateOfBirthNull())
                    row.DateOfBirth = row.DateOfBirth;
                if (!row.IsFaxNumberNull())
                    row.FaxNumber = row.FaxNumber;
                row.Culture = row.Culture;
                row.Status = row.Status;
                row.IsDeleted = false;

                proxy.UpdateUserInfo(dt);
            }

        }

        public string GetUserCulture(int userId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {

                var dt = proxy.GetUser(userId, string.Empty);
                var result = from dr in dt
                             select new
                             {
                                 culture = dr.Culture,

                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public void UpdateUserCulture(int userId, string culture)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetUser(userId, string.Empty);
                var row = dt[0];
                row.Culture = culture;
                // original data
                row.OfficeAddress = row.OfficeAddress;
                row.OfficeNumber = row.OfficeNumber;
                row.MobileNumber = row.MobileNumber;
                row.HomeNumber = row.HomeNumber;
                row.Email = row.Email;
                row.UserName = row.UserName;
                if (!row.IsGenderNull())
                    row.Gender = row.Gender;
                if (!row.IsDateOfBirthNull())
                    row.DateOfBirth = row.DateOfBirth;
                if (!row.IsFaxNumberNull())
                    row.FaxNumber = row.FaxNumber;
                row.Culture = row.Culture;
                row.Status = row.Status;
                row.IsDeleted = false;

                proxy.UpdateUserInfo(dt);
            }
        }

        // committee
        public string GetCommittee(int committeeId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetCommitteeById(committeeId);
                var result = from dr in dt
                             select new
                             {
                                 committeeName = dr.CommitteeName,
                                 committeeDesc = dr.CommitteeDescription

                             };
                return JsonConvert.SerializeObject(result);
            }

        }

        public string GetCommitteeMember(int committeeId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetCommitteeMember(committeeId);
                var result = from dr in dt
                             select new
                             {
                                 creator = dr.Creator,
                                 userName = dr.UserName,
                                 PhotoImagePath = dr.PhotoImagePath

                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public void UpdateCommitteeName(int committeeId, string committeeName)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetCommitteeById(committeeId);
                var row = dt[0];
                row.CommitteeName = committeeName;
                // original data
                row.CommitteeDescription = row.CommitteeDescription;
                row.Creator = row.Creator;
                row.IsDeleted = false;

                proxy.UpdateCommittee(dt);
            }
        }

        public void UpdateCommitteeDesc(int committeeId, string committeeDesc)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetCommitteeById(committeeId);
                var row = dt[0];
                row.CommitteeName = row.CommitteeName;
                // original data
                row.CommitteeDescription = committeeDesc;
                row.Creator = row.Creator;
                row.IsDeleted = false;

                proxy.UpdateCommittee(dt);
            }

        }

        public string IsExistCommittee(string committeeName)
        {
            int count = 0;
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                var dt = proxy.GetCommitteeByName(committeeName);
                if (dt.Rows.Count > 0)
                    count = dt.Rows.Count;
            }
            return count.ToString();
        }
    }
}